package com.chefu.juche.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chefu.common.pojo.CfException;
import com.chefu.common.pojo.ExceptionEnum;
import com.chefu.common.pojo.PageResult;
import com.chefu.juche.mapper.MerchantMapper;
import com.chefu.juche.pojo.Merchant;
import com.chefu.juche.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商家Service
 */

@Service
@Transactional
public class MerchantService {
    @Autowired
    private MerchantMapper merchantMapper;



    //根据商户ID查询商户
    public Merchant findMerchantByMerchantId(Integer merchantId) {
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant==null){
            throw new CfException(ExceptionEnum.MERCHANT_DETAIL_NOT_FOUND);
        }
        return merchant;
    }

    //根据商户ID修改商户审核状态
    public void auditMerchant(Integer merchantId, String auditStatus) {
        try {
            Merchant merchant = new Merchant();
            merchant.setMerchantId(merchantId);
            merchant.setAuditStatus(auditStatus);
            merchantMapper.updateById(merchant);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CfException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }



    public Merchant findMerchantById(long merchantId) {
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant==null){
            throw new CfException(ExceptionEnum.MERCHANT_NOT_FOUND);
        }
        return merchant;
    }

    public void updateMerchant(String fullName, String shortName, Boolean isChain, String type, String linkman, String phone, String city, int merchantId, String address, String pic, String tag) {
        Merchant merchant = new Merchant();
        merchant.setFullName(fullName);
        merchant.setShortName(shortName);
        merchant.setChain(isChain);
        merchant.setType(type);
        merchant.setLinkman(linkman);
        merchant.setPhone(phone);
        merchant.setCity(city);
        merchant.setMerchantId(merchantId);
        merchant.setAddress(address);
        merchant.setPic(pic);
        merchant.setTag(tag);
        merchantMapper.updateById(merchant);



    }

    public void saveMerchant(Merchant merchant) {
        merchantMapper.insert(merchant);

    }

    public void lockMerchantById(int merchantId) {
        Merchant merchant = new Merchant();
        merchant.setMerchantId(merchantId);
        merchant.setStatus(false);
        merchantMapper.updateById(merchant);

    }

    public List<Merchant> findAllFuwushang() {

        QueryWrapper<Merchant>queryWrapper = Wrappers.query();
        queryWrapper.eq("type","汽修厂");
        List<Merchant>merchantList=merchantMapper.selectList(queryWrapper);
        return merchantList;
    }

    public List<Merchant> findAllPeijianshang() {

        QueryWrapper<Merchant>queryWrapper = Wrappers.query();
        queryWrapper.eq("type","配件商");
        List<Merchant>merchantList=merchantMapper.selectList(queryWrapper);
        return merchantList;
    }

    public PageResult<Merchant> findMerchantByAuditStatus(Integer page, Integer rows, String fullName, Integer merchantId, String type, String city, String auditStatus, String shortName, String phone, Boolean isChain, Boolean status) {
        IPage<Merchant> ipage = new Page<>(page,rows);
        QueryWrapper<Merchant>queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(shortName)){
            queryWrapper.like("shortName",shortName);}
        if (merchantId!=null){
            queryWrapper.eq("merchantId",merchantId);}
        if (StringUtils.isNotEmpty(city)){
            queryWrapper.like("city",city);}
        if (StringUtils.isNotEmpty(phone)){
            queryWrapper.eq("phone",phone);}
        if (isChain!=null){
            queryWrapper.eq("isChain",isChain);}
        if (status!=null){
            queryWrapper.eq("status",status);}
        if (StringUtils.isNotEmpty(fullName)){
            queryWrapper.like("fullName",fullName);
        }
        if (StringUtils.isNotEmpty(auditStatus)){
            queryWrapper.eq("auditStatus",auditStatus)
                    .orderByDesc("regtime");
        }

        ipage = merchantMapper.selectPage(ipage,queryWrapper);
        PageResult<Merchant>pageResult = new PageResult<>(ipage.getTotal(),ipage.getPages(),ipage.getRecords());
        return pageResult;

    }
}
